<template>
  <div>
    <van-nav-bar title="登录" />
    <van-form @submit="onLogin">
      <van-field
        v-model="tel"
        name="手机号"
        label="手机号"
        placeholder="手机号"
        required
        :rules="rules.tel"
      />
      <van-field
        v-model="code"
        type="number"
        name="验证码"
        label="验证码"
        placeholder="验证码"
        required
        :rules="rules.code"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
// import { mapState } from 'vuex'
import { loginApi } from '@/api/login'
export default {
  name: 'Login',
  data() {
    return {
      tel: '',
      code: '',
      rules: {
        tel: [
          { required: true, message: '请填写手机号' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '别分心哦，请输入正确的手机号'
          }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          {
            pattern: /^\d{6}$/,
            message: '看仔细点哦，请输入正确的验证码'
          }
        ]
      }
    }
  },
  created() {},
  methods: {
    async onLogin() {
      try {
        const {
          data: { data }
        } = await loginApi({
          mobile: this.tel,
          code: this.code
        })
        // console.log(data)
        // this.token = data
        // console.log(this.token)
        this.$store.commit('user/setToken', data)
        this.$router.push('/')
        this.$toast('登录成功')
      } catch {
        this.$toast('登录失败')
      }
    },
    setToken() {}
  }
  //   computed:{
  //       ...mapState(['token'])
  //   }
}
</script>

<style lang="less" scoped></style>
